package cn.zsb.zsb.ctrl;

import cn.zsb.zsb.entity.Advtise;
import cn.zsb.zsb.service.AdvtiseService;
import cn.zsb.zsb.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@Controller
@RequestMapping("adv")
public class AdvController {
    @Autowired
    private AdvtiseService advtiseService;

    //applicacontext c=new classpathxml
    @GetMapping("search")
    public String page(Model m,
                       @RequestParam(required = false, name = "title") String title,
                       @RequestParam(required = false, name = "url") String url,
                       @RequestParam(required = false, name = "start") Date start,
                       @RequestParam(required = false, name = "end") Date end,
                       @RequestParam(defaultValue = "1", name = "page") int page,
                       @RequestParam(defaultValue = "10", name = "size") int size
    ) {
        Page<Advtise> search = advtiseService.search(title, url, start, end, page, size);
        m.addAttribute("p", search);
        return "advs";
    }

    @ResponseBody
    @GetMapping("search2")
    public Page<Advtise> page2(Model m,
                               @RequestParam(required = false, name = "title") String title,
                               @RequestParam(required = false, name = "url") String url,
                               @RequestParam(required = false, name = "start") Date start,
                               @RequestParam(required = false, name = "end") Date end,
                               @RequestParam(defaultValue = "1", name = "page") int page,
                               @RequestParam(defaultValue = "10", name = "size") int size
    ) {
        Page<Advtise> search = advtiseService.search(title, url, start, end, page, size);
        return search;
    }

    //    @GetMapping("del-{id}")
//    public String del(@PathVariable("id") Long id){
//        this.advtiseService.del(id);
//        return "redirect:/adv/search";
//    }
    @GetMapping("del-{id}")
    public void del(@PathVariable("id") Long id, Writer out) throws IOException {
        this.advtiseService.del(id);

        //return "redirect:/adv/search";
        out.write("<script>alert('del ok');location.replace('search');</script>");

    }

    @GetMapping("toAdd")
    public String toAdd() {
        return "addAdv";//forward:/web-inf/jsp/addAdv.jsp
    }

    @PostMapping("add")
    public String add(Advtise ad, @RequestParam(name = "a_imgurl", required = false) MultipartFile m,
                      HttpServletRequest request) {
        String images = request.getServletContext().getRealPath("images");
        if (m.isEmpty() == false) {
            String name = m.getOriginalFilename();
            String newName = name.substring(name.lastIndexOf('.'));
            newName = UUID.randomUUID() + newName;
            try {
                File folder = new File(images);
                if (!folder.exists()) {
                    folder.mkdirs();
                }
                m.transferTo(new File(folder, newName));
                ad.setImgurl(newName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.advtiseService.add(ad);
        return "redirect:/adv/search";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat2, true));
        binder.registerCustomEditor(Date.class, "addTime", new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值 }
    }
}
